package com.yy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.result.PageResult;
import com.yy.entity.*;
import com.yy.query.RegisterQuery;
import com.yy.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import com.yy.common.result.Result;

import java.util.Date;
import java.util.List;

/**
 * @Author Yang
 * @Create 2024/6/7 14:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/report")
@Tag(name = "住院管理.入院登记")
public class HospitalizationController {
    //入院
    private final ReportService reportService;
    //住院表
    private final RegisterService registerService;
    //科室
    private final DepartmentsService departmentsService;
    //医生
    private final DoctorService doctorService;
    //床位
    private final BedService bedService;
    //预交表
    private final PayService payService;
    //药房表
    private final PharmacyService pharmacyService;
    //住院收费表
    private final HospitalpriceService hospitalpriceService;
    //住院项目收费表
    private final InoutpatienttypeService inoutpatienttypeService;
    @GetMapping("/list")
    @Operation(summary = "入院登记分页查询")
    public PageResult<Report> getBookPage(int pageNum, int pageSize) {
        QueryWrapper<Report> wrapper = new QueryWrapper<>();
        wrapper.in("state", 4);
        Page<Report> page_ = new Page<Report>(pageNum, pageSize);
        return PageResult.success(reportService.page(page_, wrapper));
    }

    @GetMapping("/listRegister")
    @Operation(summary = "住院登记分页查询")
    public PageResult<Regiterlist> getRegister(RegisterQuery registerQuery) {
        IPage<Regiterlist> page = registerService.getPage(registerQuery);
        return PageResult.success(page);
    }

    @GetMapping("/getID/{reportid}")
    @Operation(summary = "入院登记ID查询")
    public Result listID(@PathVariable int reportid) {
        LambdaQueryWrapper<Report> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Report::getReportid, reportid);
        Report one = reportService.getOne(wrapper);
        return Result.success(one);
    }

    @GetMapping("/getDepart")
    @Operation(summary = "科室查询")
    public Result<List<Departments>> listDepart() {
        List<Departments> list = departmentsService.list();
        return Result.success(list);
    }

    @GetMapping("/getdepart/{departmentid}")
    @Operation(summary = "根据科室查询医生")
    public Result listDoctor(@PathVariable int departmentid) {
        LambdaQueryWrapper<Doctor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Doctor::getDepartmentid, departmentid);
        wrapper.eq(Doctor::getDstate, 1);
        List<Doctor> one = doctorService.list(wrapper);
        return Result.success(one);
    }

    @GetMapping("/getbed/{departmentid}")
    @Operation(summary = "根据科室查询床位")
    public Result listBed(@PathVariable int departmentid) {
        LambdaQueryWrapper<Bed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Bed::getDepartmentid, departmentid);
        wrapper.eq(Bed::getState, 0);
        List<Bed> one = bedService.list(wrapper);
        return Result.success(one);
    }

    @GetMapping("/updateState/{reportid}")
    @Operation(summary = "添加病人时修改病人状态")
    public Result updateState(@PathVariable int reportid) {
        boolean b = reportService.updateState(reportid);
        return Result.success(b);
    }


    @GetMapping("/updateStatebed/{bedid}")
    @Operation(summary = "添加病人时修改床位状态")
    public Result updateStatebed(@PathVariable int bedid) {
        boolean b = bedService.updateStatebed(bedid);
        return Result.success(b);
    }

    @PostMapping("/addregister")
    @Operation(summary = "添加病人")
    public Result addRegister(@RequestBody Report report) {
        System.out.println("report::" + report);
        Register register = new Register();
        BeanUtils.copyProperties(report, register);
        register.setIdcard(report.getCarid());
        register.setBednum(report.getBedid());
        register.setOperator(report.getUsers());
        register.setMoney(report.getDelivery());
        register.setRegisterdate(new Date());
        register.setDiscount(report.getDiscount());
        register.setDepartment(report.getDepartment());
        System.out.println("register::" + register);
        boolean save = registerService.save(register);
        return Result.judge(save);
    }

    @GetMapping("/getRegiterUser/{regiterid}")
    @Operation(summary = "根据住院id查询科室")
    public Result getRegiterUser(@PathVariable int regiterid) {
        List<Regiterlist> rertisterUser = registerService.getRertisterUser(regiterid);
        return Result.success(rertisterUser);
    }

    @GetMapping("/updateregisterbed/{bedids}")
    @Operation(summary = "修改转院时修改床位状态")
    public Result updateregisterbed(@PathVariable int bedids) {
        boolean b = bedService.updateregisterbed(bedids);
        return Result.success(b);
    }

    @PutMapping("/updateregister/{registerid}")
    @Operation(summary = "修改科室")
    public Result updateregister(
            @RequestBody Register register, @PathVariable int registerid) {
        boolean f = registerService.update(new LambdaUpdateWrapper<Register>()
                .eq(Register::getRegisterid, registerid)
                .set(Register::getDepartment, register.getDepartment())
                .set(Register::getDoctor, register.getDoctor())
                .set(Register::getBednum, register.getBedid())
        );
        return Result.success(f);
    }


}
